Cassava Leaf Disease Classification
Identify the type of disease present on Cassava Leaf image
This notebook is a simple training pipeline in TensorFlow for the Cassava Leaf Competition where we are given 21,397 labeled images of cassava leaves classified as 5 different groups (4 diseases and a healthy group) and asked to predict on unseen images of cassava leaves. As with most image classification problems, we can use and experiment with many different forms of augmentation and we can explore transfer learning.
import numpy as np
import pandas as pd
import seaborn as sns
import albumentations as A
import matplotlib.pyplot as plt
import os, gc, cv2, random, warnings, math, sys, json, pprint, pdb
import tensorflow as tf
from tensorflow.keras import backend as K
import tensorflow_hub as hub
from sklearn.model_selection import train_test_split
warnings.simplefilter('ignore')
print(f"Using TensorFlow v{tf.__version__}")
#@title Notebook type { run: "auto", display-mode:"form" }
SEED = 16
DEBUG = False #@param {type:"boolean"}
TRAIN = True #@param {type:"boolean"}
def seed_everything(seed=0):
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)
os.environ['PYTHONHASHSEED'] = str(seed)
os.environ['TF_DETERMINISTIC_OPS'] = '1'
GOOGLE = 'google.colab' in str(get_ipython())
KAGGLE = not GOOGLE
seed_everything(SEED)
print("Running on {}!".format(
"Google Colab" if GOOGLE else "Kaggle Kernel"
))
#@title {run: "auto", display-mode: "form" }
BASE_MODEL= 'efficientnet_b3' #@param ["'efficientnet_b3'", "'efficientnet_b4'", "'efficientnet_b2'"] {type:"raw", allow-input: true}
BATCH_SIZE = 32 #@param {type:"integer"}
HEIGHT = 300#@param {type:"number"}
WIDTH = 300#@param {type:"number"}
CHANNELS = 3#@param {type:"number"}
IMG_SIZE = (HEIGHT, WIDTH, CHANNELS)
EPOCHS = 8#@param {type:"number"}
print("Using {} with input size {}".format(BASE_MODEL, IMG_SIZE))
df = pd.read_csv(f'{input_path}train.csv')
df.head()
Check how many images are available in the training dataset and also check if each item in the training set are unique
The distribution of labels is obviously unbalanced as can be observed in the figure below.
Let's preprocess to add the directory string to the filename and rename the column to filename
df['filename'] = df['image_id'].map(lambda x : f'{input_path}train_images/{x}')
df = df.drop(columns = ['image_id'])
df = df.sample(frac=1).reset_index(drop=True)
df.head()
Let's find out what labels do we have for the 5 categories.
From the bar chart shown earlier, the label 3, Cassava Mosaic Disease (CMD) is the most common one. This imbalance may have to be addressed with a weighted loss function or oversampling. I might try this in a future iteration of this kernel or in a new kernel.
Let's check an example image to see what it looks like
Loading data
After my quick and rough EDA, let's load the PIL Image to a Numpy array, so we can move on to data augmentation.
In fastai, they have item_tfms and batch_tfms defined for their data loader API. The item transforms performs a fairly large crop to 224 and also apply other standard augmentations (in aug_tranforms) at the batch level on the GPU. The batch size is set to 32 here.
Split the dataset into training set and validation set
train_df, valid_df = train_test_split(
df
,test_size = 0.2
,random_state = SEED
,shuffle = True
,stratify = df['label'])
train_ds = tf.data.Dataset.from_tensor_slices(
(train_df.filename.values,train_df.label.values))
valid_ds = tf.data.Dataset.from_tensor_slices(
(valid_df.filename.values, valid_df.label.values))
adapt_ds = tf.data.Dataset.from_tensor_slices(
train_df.filename.values)
for x,y in valid_ds.take(3): print(x, y)
def decode_image(filename):
img = tf.io.read_file(filename)
img = tf.image.decode_jpeg(img, channels=3)
return img
def collate_train(filename, label):
img = decode_image(filename)
img = tf.image.random_brightness(img, 0.3)
img = tf.image.random_flip_left_right(img, seed=None)
img = tf.image.random_crop(img, IMG_SIZE)
return img, label
def process_adapt(filename):
img = decode_image(filename)
img = tf.keras.layers.experimental.preprocessing.Rescaling(1.0 / 255)(img)
return img
def collate_valid(filename, label):
img = decode_image(filename)
img = tf.image.random_crop(img, IMG_SIZE)
return img, label
train_ds = train_ds.map(collate_train, num_parallel_calls=AUTOTUNE)
valid_ds = valid_ds.map(collate_valid, num_parallel_calls=AUTOTUNE)
adapt_ds = adapt_ds.map(process_adapt, num_parallel_calls=AUTOTUNE)
train_ds_batch = (train_ds
.cache('dump.tfcache')
.shuffle(buffer_size=1000)
.batch(BATCH_SIZE)
.prefetch(buffer_size=AUTOTUNE))
valid_ds_batch = (valid_ds
#.shuffle(buffer_size=1000)
.batch(BATCH_SIZE*2)
.prefetch(buffer_size=AUTOTUNE))
adapt_ds_batch = (adapt_ds
.shuffle(buffer_size=1000)
.batch(BATCH_SIZE)
.prefetch(buffer_size=AUTOTUNE))
def show_images(ds):
_,axs = plt.subplots(3,3,figsize=(16,16))
for ((x, y), ax) in zip(ds.take(9), axs.flatten()):
ax.imshow(x.numpy().astype(np.uint8))
ax.set_title(np.argmax(y))
ax.axis('off')
Show some training images
Show some validation images
data_augmentation = tf.keras.Sequential(
[
tf.keras.layers.experimental.preprocessing.RandomCrop(HEIGHT, WIDTH),
tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
tf.keras.layers.experimental.preprocessing.RandomRotation(0.25),
tf.keras.layers.experimental.preprocessing.RandomZoom((-0.2, 0)),
tf.keras.layers.experimental.preprocessing.RandomContrast((0.2,0.2))
]
)
func = lambda x,y: (data_augmentation(x), y)
x = (train_ds
.batch(BATCH_SIZE)
.take(1)
.map(func, num_parallel_calls=AUTOTUNE))
show_images(x.unbatch())
%%run_if {GOOGLE}
from tensorflow.keras.applications import EfficientNetB3
from tensorflow.keras.applications import VGG16
def build_model(base_model, num_class):
inputs = tf.keras.layers.Input(shape=IMG_SIZE)
x = data_augmentation(inputs)
x = base_model(x)
x = tf.keras.layers.Dropout(0.4)(x)
outputs = tf.keras.layers.Dense(num_class, activation="softmax", name="pred")(x)
model = tf.keras.models.Model(inputs=inputs, outputs=outputs)
return model
efficientnet = EfficientNetB3(
weights = 'imagenet' if TRAIN else None,
include_top = False,
input_shape = IMG_SIZE,
pooling='avg')
efficientnet.trainable = True
model = build_model(base_model=efficientnet, num_class=len(id2label))
model.summary()
The 3rd layer of the Efficient is the Normalization layer, which can be tuned to our new dataset instead of imagenet. Be patient on this one, it does take a bit of time as we're going through the entire training set.
%%run_if {GOOGLE and TRAIN}
if not os.path.exists("000_normalization.h5"):
model.get_layer('efficientnetb3').get_layer('normalization').adapt(adapt_ds_batch)
model.save_weights("000_normalization.h5")
else:
model.load_weights("000_normalization.h5")
CosineDecayRestarts function implemented in tf.keras as it seemed promising and I struggled to find the right settings (if there were any) for the ReduceLROnPlateau
%%run_if {TRAIN}
#@title { run: "auto", display-mode: "form" }
STEPS = math.ceil(len(train_df) / BATCH_SIZE) * EPOCHS
LR_START = 9e-3 #@param {type: "number"}
LR_START *= strategy.num_replicas_in_sync
LR_MIN = 3e-4 #@param {type: "number"}
N_RESTARTS = 5#@param {type: "number"}
T_MUL = 2.0 #@param {type: "number"}
M_MUL = 1#@param {type: "number"}
STEPS_START = math.ceil((T_MUL-1)/(T_MUL**(N_RESTARTS+1)-1) * STEPS)
schedule = tf.keras.experimental.CosineDecayRestarts(
first_decay_steps=STEPS_START,
initial_learning_rate=LR_START,
alpha=LR_MIN,
m_mul=M_MUL,
t_mul=T_MUL)
x = [i for i in range(STEPS)]
y = [schedule(s) for s in range(STEPS)]
_,ax = plt.subplots(1,1,figsize=(8,5),facecolor='#F0F0F0')
ax.plot(x, y)
ax.set_facecolor('#F8F8F8')
ax.set_xlabel('iteration')
ax.set_ylabel('learning rate')
print('{:d} total epochs and {:d} steps per epoch'
.format(EPOCHS, STEPS // EPOCHS))
print(schedule.get_config())
LearningRateScheduler that tensorflow gives us. The LearningRateScheduler update the lr on_epoch_begin while it makes more sense to do it on_batch_end or on_batch_begin.
%%run_if {GOOGLE and TRAIN}
from tensorflow.keras.callbacks import Callback
class LRFinder(Callback):
"""`Callback` that exponentially adjusts the learning rate after
each training batch between `start_lr` and `end_lr` for a maximum number
of batches: `max_step`. The loss and learning rate are recorded at each
step allowing visually finding a good learning rate as
https://sgugger.github.io/how-do-you-find-a-good-learning-rate.html suggested.
"""
def __init__(self, start_lr: float = 1e-7, end_lr: float = 10,
max_steps: int = 100, smoothing=0.9):
super(LRFinder, self).__init__()
self.start_lr, self.end_lr = start_lr, end_lr
self.max_steps = max_steps
self.smoothing = smoothing
self.step, self.best_loss, self.avg_loss, self.lr = 0, 0, 0, 0
self.lrs, self.losses = [], []
def on_train_begin(self, logs=None):
self.step, self.best_loss, self.avg_loss, self.lr = 0, 0, 0, 0
self.lrs, self.losses = [], []
def on_train_batch_begin(self, batch, logs=None):
self.lr = self.exp_annealing(self.step)
tf.keras.backend.set_value(self.model.optimizer.lr, self.lr)
def on_train_batch_end(self, batch, logs=None):
logs = logs or {}
loss = logs.get('loss')
step = self.step
if loss:
self.avg_loss = self.smoothing * self.avg_loss + (1 - self.smoothing) * loss
smooth_loss = self.avg_loss / (1 - self.smoothing ** (self.step + 1))
self.losses.append(smooth_loss)
self.lrs.append(self.lr)
if step == 0 or loss < self.best_loss:
self.best_loss = loss
if smooth_loss > 4 * self.best_loss or tf.math.is_nan(smooth_loss):
self.model.stop_training = True
if step == self.max_steps:
self.model.stop_training = True
self.step += 1
def exp_annealing(self, step):
return self.start_lr * (self.end_lr / self.start_lr) ** (step * 1. / self.max_steps)
def plot(self, skip_end=None):
lrs = self.lrs[:-skip_end] if skip_end else self.lrs[:-5]
losses = self.losses[:-skip_end] if skip_end else self.losses[:-5]
fig, ax = plt.subplots(1, 1, facecolor="#F0F0F0")
ax.set_ylabel('Loss')
ax.set_xlabel('Learning Rate')
ax.set_xscale('log')
ax.xaxis.set_major_formatter(plt.FormatStrFormatter('%.0e'))
ax.plot(lrs, losses)
%%run_if {GOOGLE and TRAIN}
model.compile(loss="sparse_categorical_crossentropy",
optimizer="adam",
metrics=["accuracy"])
lr_finder = LRFinder()
_ = model.fit(train_ds_batch, epochs=1, callbacks=[lr_finder])
%%run_if {GOOGLE and TRAIN}
lr_finder.plot(skip_end=20)
As can be observed from the curve, we can pinpoint the lr_max to be 9e-3 and the lr_min to be 3e-4. Let's feed these hyperparams back to the optimizer schedule and retrain the model.
Before retraining, don't forget to reset the model so it can be trained from the 000_normalization.h5 rather than 1 epoch after it because executing the lr_finder
tflearner and have this implemented as a .reset method of a learner class.
%%run_if {GOOGLE and TRAIN}
efficientnet = EfficientNetB3(
weights = 'imagenet',
include_top = False,
input_shape = IMG_SIZE,
pooling='avg')
efficientnet.trainable = True
model = build_model(base_model=efficientnet, num_class=len(id2label))
model.load_weights("000_normalization.h5")
%%run_if {TRAIN}
callbacks = [
tf.keras.callbacks.ModelCheckpoint(
filepath='001_best_model.h5',
monitor='val_loss',
save_best_only=True),
]
%%run_if {TRAIN}
model.compile(loss="sparse_categorical_crossentropy",
optimizer=tf.keras.optimizers.Adam(schedule),
metrics=["accuracy"])
%%run_if {TRAIN}
history = model.fit(train_ds_batch,
epochs = EPOCHS,
validation_data=valid_ds_batch,
callbacks=callbacks)
def show_history(history):
topics = ['loss', 'accuracy']
groups = [{k:v for (k,v) in history.items() if topic in k} for topic in topics]
_,axs = plt.subplots(1,2,figsize=(15,6),facecolor='#F0F0F0')
for topic,group,ax in zip(topics,groups,axs.flatten()):
for (_,v) in group.items(): ax.plot(v)
ax.set_facecolor('#F8F8F8')
ax.set_title(f'{topic} over epochs')
ax.set_xlabel('epoch')
ax.set_ylabel(topic)
ax.legend(['train', 'valid'], loc='best')
%%run_if {TRAIN}
show_history(history.history)
We load the best weight that were kept from the training phase. Just to check how our model is performing, we will attempt predictions over the validation set. This can help to highlight any classes that will be consistently miscategorised.
model.load_weights('{}001_best_model.h5'.format(
'' if TRAIN else '../input/cassava-leaf-disease-classification-models/'))
x = train_df.sample(1).filename.values[0]
img = decode_image(x)
%%time
imgs = [tf.image.random_crop(img, size=IMG_SIZE) for _ in range(4)]
_,axs = plt.subplots(1,4,figsize=(16,4))
for (x, ax) in zip(imgs, axs.flatten()):
ax.imshow(x.numpy().astype(np.uint8))
ax.axis('off')
I apply some very basic test time augmentation to every local image extracted from the original 600-by-800 images. We know we can do some fancy augmentation with albumentations but I wanted to do that exclusively with Keras preprocessing layers to keep the cleanest pipeline possible.
tta = tf.keras.Sequential(
[
tf.keras.layers.experimental.preprocessing.RandomCrop(HEIGHT, WIDTH),
tf.keras.layers.experimental.preprocessing.RandomFlip("horizontal_and_vertical"),
tf.keras.layers.experimental.preprocessing.RandomZoom((-0.2, 0.2)),
tf.keras.layers.experimental.preprocessing.RandomContrast((0.2,0.2))
]
)
def predict_tta(filename, num_tta=4):
img = decode_image(filename)
img = tf.expand_dims(img, 0)
imgs = tf.concat([tta(img) for _ in range(num_tta)], 0)
preds = model.predict(imgs)
return preds.sum(0).argmax()
pred = predict_tta(df.sample(1).filename.values[0])
print(pred)
from tqdm import tqdm
preds = []
with tqdm(total=len(valid_df)) as pbar:
for filename in valid_df.filename:
pbar.update()
preds.append(predict_tta(filename, num_tta=4))
cm = tf.math.confusion_matrix(valid_df.label.values, np.array(preds))
plt.figure(figsize=(10, 8))
sns.heatmap(cm,
xticklabels=id2label.values(),
yticklabels=id2label.values(),
annot=True,
fmt='g',
cmap="Blues")
plt.xlabel('Prediction')
plt.ylabel('Label')
plt.show()
test_folder = input_path + '/test_images/'
submission_df = pd.DataFrame(columns={"image_id","label"})
submission_df["image_id"] = os.listdir(test_folder)
submission_df["label"] = 0
submission_df['label'] = (submission_df['image_id']
.map(lambda x : predict_tta(test_folder+x)))
submission_df
submission_df.to_csv("submission.csv", index=False)